﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace FACUI.BasicClass
{
    public delegate void RevDataEvevtHandler(Object sender, RevEventArgs e);
    public class RevEventArgs:EventArgs
    {
        public int iProcessCode=0;//处理主机编号
        public int iSensorCode=0;//传感器编号
        public bool bSensorCode=false;//传感器数据
        public string strGDDLID=string.Empty;//轨道电路ID
        public bool bGDDLCode=false;//轨道电路数据
    }
    public class GetData
    {
        private UdpClient _udp = null;
        private IPEndPoint _ipe=null;
        //测算速度
        private Queue<string> strQueRecDateTime = new Queue<string>(3600);
        private double dtmp = 0;

        public GetData(int iRecPort)
        {
            try
            {
                _udp = new UdpClient(iRecPort);
                _ipe = new IPEndPoint(IPAddress.Any, iRecPort);
            }
            catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.StackTrace+":"+ex.Message ); }
        }
        private bool isEqualsArray(byte[] bytes1,byte[] bytes2)
        {
            if (bytes1 == bytes2)
            {
                return true;
            }
            if (bytes1 == null || bytes2 == null)
            {
                return false;
            }
            if (bytes1.Length != bytes2.Length)
            {
                return false;
            }
            else
            {
                for (int i = 0; i < bytes1.Length; i++)
                {
                    if (bytes1[i] != bytes2[i])
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        public void ReceiveData()
        {
            byte[] btmp = null;
            
            while (true)
            {
                try
                {
                    byte[] bytes = _udp.Receive(ref _ipe);
                    if (!isEqualsArray(btmp,bytes))
                    {
                        //有改变的数据包的接收时间
                        string strCurTime=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff");
                        strQueRecDateTime.Enqueue(strCurTime);
                        if (strQueRecDateTime.Count > 0)
                        {
                            dtmp = (DateTime.Parse(strCurTime) - DateTime.Parse(strQueRecDateTime.Peek())).TotalSeconds;
                            if (dtmp < 15)
                            {
                                if (strQueRecDateTime.Count >1)
                                {
                                    string[] strArray = strQueRecDateTime.ToArray();
                                    CGlobal.dTimeSpan = (DateTime.Parse(strArray[1]) - DateTime.Parse(strArray[0])).TotalSeconds;
                                }
                             }
                            else
                            {   
                                //删除接收时间在15S外的变化数据包计数
                                strQueRecDateTime.Clear();
                                //添加最近的变化数据包接收时间
                                strQueRecDateTime.Enqueue(strCurTime);
                            }
                        }
                        CGlobal.AddRecData(bytes);
                        btmp = bytes;
                    }
                    else if (CGlobal.GetWaitCount() < 4)
                    {
                        CGlobal.AddRecData(bytes);
                    }
                }
                catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.StackTrace + ":" + ex.Message); }
            }
        }
    }
}
